extern crate actix_web;
#[macro_use]
extern crate serde_json;
use actix_web::http::Method;
use std::sync::{Arc, Mutex};


mod req_handlers;
mod dragonpak;

fn main() {
    //打开/新建一个数据库
    let connection = dragonpak::open_database("data/db/dragonpak.db").or_else(|err| {
        println!("{}", err);
        Err(err)
    }).unwrap();

    let share_state = Arc::new(Mutex::new(dragonpak::ShareState {
        connection,
        //tokens: vec![]
    }));

    let sys = actix_web::actix::System::new("dragonpak");

    let server = actix_web::server::new(move || {
        //用于提供API服务的APP
        let app_api = actix_web::App::with_state(share_state.clone())
            .prefix("/api")
            // article
            .resource("/article", |r| {
                r.method(Method::GET).f(req_handlers::article_get);
                //r.method()
            })
            .resource("/article/class", |r| {
                r.method(Method::GET).f(req_handlers::article_class_get);
            })
            .resource("/article/{CLASS}", |r| {
                //r.method(Method::GET).f(req_handlers::article_CLASS_get);
                r.method(Method::GET).f(req_handlers::article_CLASS_get);
                r.method(Method::POST).with(req_handlers::article_CLASS_post);
            })
            .resource("/article/{CLASS}/{ID}", |r| {
                r.method(Method::GET).with(req_handlers::article_CLASS_ID_get);
                r.method(Method::POST).with(req_handlers::article_CLASS_ID_post);
                r.method(Method::DELETE).with(req_handlers::article_CLASS_ID_delete);
            })
            .resource("/article/{CLASS}/{ID}/{IMG}", |r| {
                r.method(Method::GET).with(req_handlers::article_CLASS_ID_IMG_get);
                r.method(Method::POST).with(req_handlers::article_CLASS_ID_IMG_post);
            })
            //app
            .resource("/app", |r| {
                r.method(Method::GET).f(req_handlers::app_get);

            })
            .resource("/app/class", |r| {
                r.method(Method::GET).f(req_handlers::app_class_get);
            })
            .resource("/app/{CLASS}", |r| {
                r.method(Method::GET).f(req_handlers::app_CLASS_get);
                r.method(Method::POST).with(req_handlers::app_CLASS_post);
            })
            .resource("/app/{CLASS}/{ID}", |r| {
                r.method(Method::GET).with(req_handlers::app_CLASS_ID_get);
                r.method(Method::POST).with(req_handlers::app_CLASS_ID_post);
                r.method(Method::DELETE).with(req_handlers::app_CLASS_ID_delete);
            })
            .resource("/app/{CLASS}/{ID}/{IMG}", |r| {
                r.method(Method::GET).with(req_handlers::app_CLASS_ID_IMG_get);
                r.method(Method::POST).with(req_handlers::app_CLASS_ID_IMG_post);
            })
            .resource("/user", |r| {
                r.method(Method::POST).with(req_handlers::user_post);
                r.method(Method::DELETE).with(req_handlers::user_delete);
                r.method(Method::GET).with(req_handlers::user_get);
            }).finish();
        //let app app_api.

        //在根路径下挂载静态文件的APP
        let app_static_file = actix_web::App::with_state(share_state.clone())
            .prefix("/")
            .handler("/", actix_web::fs::StaticFiles::new("./data/static")
                .unwrap()
                .index_file("index.html"))
            .finish();
            //.resource("/", |r| ());

        //let app_root = actix_web::App::new()
        //    .resource("/", |r| r.with(handler));

        vec![app_api, app_static_file]
    }).bind("127.0.0.1:8088")
        .expect("can not bind ip");

    let addr_server = server.start();

    let _ = sys.run();

}
